//
//  IGArtistCell.m
//  jingdongfang
//
//  Created by BOE on 2017/8/28.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGArtistCell.h"

static NSString *const kIGArtistCell = @"kIGArtistCell";
@interface IGArtistCell ()

@property (nonatomic, strong) UIImageView *headerImgView;
@property (nonatomic, strong) UILabel *userNameLable;
@property (nonatomic, strong) UILabel *zuoPinNumLabel;
@property (nonatomic, strong) UILabel *fenSiNumLabel;
@property (nonatomic, strong) UIButton *followBtn;

@end

@implementation IGArtistCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (IGArtistCell *)cellWithTableView:(UITableView *)tableView {
    IGArtistCell *cell = [tableView dequeueReusableCellWithIdentifier:kIGArtistCell];
    if (!cell) {
        cell = [[IGArtistCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIGArtistCell];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}

- (void)createUI {
    _headerImgView = [UIImageView new];
    _headerImgView.backgroundColor = [UIColor mainRandomColor];
    _headerImgView.layer.masksToBounds = YES;
    _headerImgView.layer.cornerRadius = 17;
    [self.contentView addSubview:_headerImgView];
    
    _userNameLable = [UILabel new];
    _userNameLable.font = [UIFont font16];
    _userNameLable.textColor = [UIColor c4Color];
    [self.contentView addSubview:_userNameLable];
    
    _zuoPinNumLabel = [UILabel new];
    _zuoPinNumLabel.font = [UIFont font12];
    _zuoPinNumLabel.textColor = [UIColor c5Color];
    [self.contentView addSubview:_zuoPinNumLabel];
    
    _fenSiNumLabel = [UILabel new];
    _fenSiNumLabel.font = [UIFont font12];
    _fenSiNumLabel.textColor = [UIColor c5Color];
    [self.contentView addSubview:_fenSiNumLabel];
    
    _followBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _followBtn.backgroundColor = [UIColor mainBackgroundColor];
//    [_followBtn setTitle:@"+关注" forState:UIControlStateNormal];
    [_followBtn setTitle:NSLocalizedString(@"Follow", nil) forState:UIControlStateNormal];
    [_followBtn setTitleColor:[UIColor c3Color] forState:UIControlStateNormal];
//    [_followBtn setTitle:@"已关注" forState:UIControlStateSelected];
    [_followBtn setTitle:NSLocalizedString(@"Followed", nil) forState:UIControlStateSelected];
    [_followBtn setTitleColor:[UIColor c5Color] forState:UIControlStateSelected];
    _followBtn.layer.masksToBounds = YES;
    _followBtn.layer.cornerRadius = 15.0;
    _followBtn.titleLabel.font = [UIFont font14];
    [self.contentView addSubview:_followBtn];
    __weak typeof(self) weakSelf = self;
    [_followBtn setTapActionWithBlock:^{
        if (weakSelf.attenBlock) {
            weakSelf.attenBlock();
        }
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor mainBackgroundColor];
    [self.contentView addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(5);
    }];
    
    [_headerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kGAP15);
        make.top.mas_equalTo(12.5);
        make.width.height.mas_equalTo(35);
    }];
    
    [_userNameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kGAP15);
        make.left.mas_equalTo(_headerImgView.mas_right).offset(10);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(15);
    }];
    
    [_zuoPinNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_userNameLable);
        make.top.mas_equalTo(_userNameLable.mas_bottom).offset(5);
        make.bottom.mas_equalTo(lineView.mas_top).offset(-10);
        make.width.mas_equalTo(100);
    }];
    
    [_fenSiNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_zuoPinNumLabel.mas_right).offset(8);
        make.top.mas_equalTo(_zuoPinNumLabel);
        make.bottom.mas_equalTo(_zuoPinNumLabel);
        make.width.mas_equalTo(100);
    }];
    
    [_followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kGAP15);
        make.centerY.mas_equalTo(_headerImgView.mas_centerY);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(30);
    }];
}

- (void)setUserInfo:(IGUserInfo *)userInfo {
    _userInfo = userInfo;
    
    [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:userInfo.image]];
    self.userNameLable.text = userInfo.nike;
//    NSString *zuoPinStr = [NSString stringWithFormat:@"作品 %@", userInfo.worksNums];
    NSString *zuoPinStr = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Artworks", nil),userInfo.worksNums];

    CGFloat zuoPinW = [zuoPinStr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, _zuoPinNumLabel.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont font12]} context:nil].size.width + 0.5;
    self.zuoPinNumLabel.text = zuoPinStr;
    [self.zuoPinNumLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(zuoPinW);
    }];
    
//    NSString *fenSiStr = [NSString stringWithFormat:@"粉丝 %@", userInfo.followNums];
    NSString *fenSiStr = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Fans", nil),userInfo.followNums];
    CGFloat fenSiW = [zuoPinStr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, _fenSiNumLabel.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont font12]} context:nil].size.width + 0.5;
    self.fenSiNumLabel.text = fenSiStr;
    [self.fenSiNumLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(fenSiW);
    }];
    
    if ([userInfo.attentionState isEqualToString:@"1"]) {
        _followBtn.selected = YES;
        _followBtn.layer.borderWidth = 1.0;
        _followBtn.layer.borderColor = [UIColor c5Color].CGColor;
        _followBtn.backgroundColor = [UIColor c3Color];
        
    } else {
        _followBtn.selected = NO;
        _followBtn.layer.borderWidth = 0.f;
        _followBtn.layer.borderColor = [UIColor appColor].CGColor;
        _followBtn.backgroundColor = [UIColor appColor];
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
